/*
 * GuildFraktionInfo - list the reputation and the guild level of all members of a wow guild
 * Copyright (C) [2012 - 2013] [www.marantis.de]
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, 
 * see <http://www.gnu.org/licenses/>.
 */
package de.marantis.app.wow.gfi.helper;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import org.apache.log4j.Logger;

public class CommonHelper {

    private final static Logger log = Logger.getLogger(CommonHelper.class);

    /**
     * Convert a string to an integer value.<br>
     * <br>
     * Normally i'll return a 0 on errors or a null/empty string but the 0 is still 
     * reserved in the reputation levels. So i'll return -1 if somethings goes wrong.
     */
    public static int String2Int(String value) {
        try {
            if (value == null || value.length() <= 0) {
                return -1;
            }

            int i = Integer.parseInt(value);
            return i;

        } catch (Exception e) {
            log.error(e);
            return -1;
        }
    }

    /**
     * Convert a string to an integer value.
     */
    public static boolean String2Boolean(String value) {
        try {
            if (value == null || value.length() <= 0) {
                return false;
            }

            boolean b = Boolean.parseBoolean(value);
            return b;

        } catch (Exception e) {
            log.error(e);
            return false;
        }
    }

    /**
     * Trim a string ...
     * If it's empty return NULL
     * or return the string.
     */
    public static String Trim2Null(String value) {
        try {
            if (value == null) {
                return null;
            }
            
            value = value.trim();
            if (value.length() <= 0) {
                return null;
            }
            
            return value;

        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

    /**
     * delete the ? (cgi) options from a string
     */
    public static String getUrlWithoutCGI(String url) {
        try {
            if (url == null) {
                return null;
            }

            url = url.trim();
            if (url.length() <= 0) {
                return null;
            }

            int pos1 = url.indexOf("?");
            if (pos1 < 0) {
                return url;
            }

            url = url.substring(0, pos1);
            return url;

        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

    public static boolean writeFileText(File file, String newText) {
        try {
            if (file == null) {
                return true;
            }

            file.createNewFile();

            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            out.write(newText);
            out.flush();
            out.close();
            return true;

        } catch (Exception e) {
            log.error(e);
            return false;
        }
    }

    public static boolean replaceFileText(File file, String oldText, String newText) {
        try {
            String line;
            StringBuilder sb = new StringBuilder("");

            BufferedReader reader = new BufferedReader(new FileReader(file));
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\r\n");
            }

            reader.close();

            String fileText = sb.toString();
            fileText = fileText.replaceAll(oldText, newText);

            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            out.write(fileText);
            out.flush();
            out.close();

            return true;

        } catch (Exception e) {
            log.error(e);
            return false;
        }
    }
}
